﻿@model Think9.Models.RuleListEntity
@{ ViewBag.Title = "Index";
    Layout = "~/Areas/Shared/_LayuiList.cshtml"; }

<blockquote class="layui-elem-quote layui-text">
    为录入表指标指定数据规范，可实现数据自动填写(如为文本框赋值)、选择输入(如为下拉框、弹出选择设置数据源)。使用步骤： 1：先定义; 2：为录入表指标指定数据规范。
</blockquote>
<input style="display: none" name="classid" id="classid" value="@ViewBag.ClassID">
<div class="layui-fluid">
    <div class="layui-row layui-col-space10">
        <div class="layui-col-md2" style="padding-top: 15px; padding-right: 20px;">
            <fieldset class="table-fieldset">

                <div id="div_A">
                    <label class="layui-form-label">
                        <a id="a1" style="font-weight:bold">  <span class="layui-left-nav"><i class="fa fa-cog"></i>系统指标</span></a>
                    </label>
                    <label class="layui-form-label">
                        <a id="a2">  <span class="layui-left-nav"><i class="fa fa-edit"></i>自动编号</span></a>
                    </label>
                    <label class="layui-form-label">
                        <a id="a6">  <span class="layui-left-nav"><i class="fa fa-reorder"></i>单列选择</span></a>
                    </label>
                    <label class="layui-form-label">
                        <a id="a7">  <span class="layui-left-nav"><i class="fa fa-list-ul"></i>多列选择</span></a>
                    </label>
                    <label class="layui-form-label">
                        <a id="a8">  <span class="layui-left-nav"><i class="fa fa-sort-amount-asc"></i>树形选择</span></a>
                    </label>
                </div>
            </fieldset>
            <br />
        </div>
        <div class="layui-col-md10 layui-col-xs7">
            <fieldset class="table-fieldset" id="searchfield" style="display: none;">
                <legend>搜索</legend>
                <div style="margin: 5px 5px 5px 5px">
                    <form class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <input type="text" name="key" id="key" placeholder="名称" class="layui-input">
                            </div>
                            <div class="layui-inline">
                                <button type="button" class="layui-btn layui-btn-normal" id="search"><i class="layui-icon layui-icon-search"></i></button>
                                <button type='reset' class='layui-btn layui-btn-primary'><i class='layui-icon layui-icon-refresh'></i></button>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>
            <table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
        </div>
    </div>
</div>
<script>
    layui.use(["table", "form", "exLayer", "exUtils", "miniPage"], function () {
        let table = layui.table;
        let form = layui.form;
        let exLayer = layui.exLayer;
        let exUtils = layui.exUtils;
        let miniPage = layui.miniPage;

        var openWH = miniPage.getOpenWidthHeight();
        let $ = layui.jquery;

        $("#a1").click(function () {
            $("#classid").val("1");
            ThisTable.reload({
                url: "/SysTable/RuleList/GetList?classid=" + $("#classid").val(),
                page: { curr: 1 }
            });
            setFont();
            $("#a1").css("font-weight", "Bold");
            return false;
        });

        $("#a2").click(function () {
            $("#classid").val("2");
            ThisTable.reload({
                url: "/SysTable/RuleList/GetList?classid=" + $("#classid").val(),
                page: { curr: 1 }
            });
            setFont();
            $("#a2").css("font-weight", "Bold");
            return false;
        });

        $("#a6").click(function () {
            $("#classid").val("6");
            ThisTable.reload({
                url: "/SysTable/RuleList/GetList?classid=" + $("#classid").val(),
                page: { curr: 1 }
            });
            setFont();
            $("#a6").css("font-weight", "Bold");
            return false;
        });

        $("#a7").click(function () {
            $("#classid").val("7");
            ThisTable.reload({
                url: "/SysTable/RuleList/GetList?classid=" + $("#classid").val(),
                page: { curr: 1 }
            });
            setFont();
            $("#a7").css("font-weight", "Bold");
            return false;
        });

        $("#a8").click(function () {
            $("#classid").val("8");
            ThisTable.reload({
                url: "/SysTable/RuleList/GetList?classid=" + $("#classid").val(),
                page: { curr: 1 }
            });
            setFont();
            $("#a8").css("font-weight", "Bold");
            return false;
        });

        let ThisTable = table.render({
            elem: "#tableId",
            url: "/SysTable/RuleList/GetList?classid=" + $("#classid").val(),
            limit: 10,
            page: true,
            defaultToolbar: [{ title: '搜索', layEvent: 'searchShow', icon: 'layui-icon-search' }, { title: '刷新', layEvent: 'refresh', icon: 'layui-icon-refresh' }, 'filter', 'exports'],
            toolbar: "#toolbarTpl",
            cols: [[
                { type: "checkbox" },
                { field: "RuleName", title: "名称", width: 150, sort: true },
                { field: "Used", title: "已使用", sort: true },
                { title: "操作", width: 120, align: "center", fixed: "right", templet: "#operationTpl" }
            ]],
            done: function (res, curr, count) {
                set();
                console.log(res, curr, count);
            }
        });

        table.on("toolbar(tableFilter)", function (obj) {
            switch (obj.event) {
                case "add":
                    add();
                    break;
                case "batchDel":
                    batchDel();
                    break;
                case "searchShow":
                    searchShow();
                    break;
                case "refresh":
                    refresh();
                    break;
            }
        });

        table.on("tool(tableFilter)", function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case "edit":
                    edit(data.RuleId, data.RuleType);
                    break;
                case "del":
                    del(data.RuleId);
                    break;
                case "searchShow":
                    searchShow();
                    break;
            }
        });

        //查询
        $("#search").click(function () {
            search();
        });

        function batchDel() {
            exLayer.confirm("确定要批量删除吗？", function (index) {
                layer.close(index);
                var idsStr = "";
                var checkStatus = table.checkStatus("tableId");
                var rows = checkStatus.data.length;
                if (rows > 0) {
                    for (var i = 0; i < checkStatus.data.length; i++) {
                        idsStr += checkStatus.data[i].RuleId + ",";
                    }
                } else {
                    layer.msg("未选择有效数据");
                }
                if (idsStr) {
                    exUtils.ajax("/SysTable/RuleList/BatchDel", "get", { idsStr: idsStr }, true).done(function (response) {
                        exUtils.tableSuccessMsg(response.message);
                        $(".layui-laypage-btn")[0].click();
                    }).fail(function (error) {
                        console.log(error);
                    });
                }
            });
        }

        function searchShow() {
            var display = $('#searchfield').css('display');
            if (display == 'none') {
                $("#searchfield").show();
            }
            else {
                $("#searchfield").hide();
            }
        }

        function search() {
            $("#searchfield").hide();
            ThisTable.reload({
                where: { key: $("#key").val() },
                page: { curr: 1 }
            });
            return false;
        }

        function set() {
            var classname = "";
            if ($("#classid").val() == "2") {
                classname = "自动编号";
            }
            if ($("#classid").val() == "6") {
                classname = "单列选择";
            }
            if ($("#classid").val() == "7") {
                classname = "多列选择";
            }
            if ($("#classid").val() == "8") {
                classname = "树形选择";
            }
            $("#add").attr("style", "display:none;");
            $("#batchDel").attr("style", "display:none;");
            if (classname != '') {
                $("#add").attr("style", "display:block;");
                $("#add").html("<i class='fa fa-plus'></i>新增" + classname);
                $("#batchDel").attr("style", "display:block;");
            }
        }

        function add() {
            if ($("#classid").val() == '2') {
                exUtils.ajax("/SysBasic/AutoCode/NewGuid", "post", {}, true).done(function (response) {
                    var rid = response.extra;
                    exLayer.open("添加自动编号", "/SysTable/RuleAuto/List?N=Y&rid=" + rid, openWH[0] + 'px', openWH[1] + 'px', openWH[2] + 'px', openWH[3] + 'px', null, null);
                });
            }
            if ($("#classid").val() == '6') {

                exLayer.open("添加单列选择", "/SysTable/RuleSingle/Add", '100%', '100%', '0px', '0px', null, null);
            }
            if ($("#classid").val() == '7') {

                exLayer.open("添加多列选择", "/SysTable/RuleMultiple/Add", '100%', '100%', '0px', '0px', null, null);
            }
            if ($("#classid").val() == '8') {

                exLayer.open("添加树形选择", "/SysTable/RuleTree/Add", '100%', '100%', '0px', '0px', null, null);
            }
        }

        function refresh() {
            $("#key").val('');
            ThisTable.reload({
                url: "/SysTable/RuleList/GetList?classid=" + $("#classid").val() + "&key=",
                page: { curr: 1 }
            });
        }

        function edit(id, classid) {
            if (classid == '2') {
                exLayer.open("编辑自动编号", "/SysTable/RuleAuto/List?rid=" + id, openWH[0] + 'px', openWH[1] + 'px', openWH[2] + 'px', openWH[3] + 'px', null, null);
            }
            if (classid == '6') {
                exLayer.open("编辑单列选择", "/SysTable/RuleSingle/Edit?rid=" + id, '100%', '100%', '0px', '0px', null, null);
            }
            if (classid == '7') {
                exLayer.open("编辑多列选择", "/SysTable/RuleMultiple/Edit?rid=" + id, '100%', '100%', '0px', '0px', null, null);
            }
            if (classid == '8') {
                exLayer.open("编辑树形选择", "/SysTable/RuleTree/Edit?rid=" + id, '100%', '100%', '0px', '0px', null, null);
            }
        }

        function del(id) {
            exLayer.confirm("确定要删除吗？", function () {
                exUtils.ajax("/SysTable/RuleList/Delete", "get", { id: id }, true).done(function (response) {
                    exUtils.tableSuccessMsg(response.message);
                    $(".layui-laypage-btn")[0].click();
                }).fail(function (error) {
                    console.log(error);
                });
            })
        }

        function setFont() {
            $("#a1").css("font-weight", "normal");
            $("#a2").css("font-weight", "normal");
            $("#a6").css("font-weight", "normal");
            $("#a7").css("font-weight", "normal");
            $("#a8").css("font-weight", "normal");
        }
    })</script>
<!-- 头工具栏模板 -->
<script type="text/html" id="toolbarTpl">
    <div class="layui-inline"><button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="add" id="add"><i class="fa fa-plus"></i>新增</button></div>
    <div class="layui-inline"><button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="batchDel" id="batchDel"><i class="fa fa-close"></i>批量删除</button></div>
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="operationTpl">
    {{# if (d.RuleType == '1') { }}

    {{#  } else{ }}
    <a class='layui-btn layui-btn-normal layui-btn-xs' lay-event='edit' id='edit'>编辑</a>
    <a class='layui-btn layui-btn-danger layui-btn-xs' lay-event='del' id='del'>删除</a>
    {{#  } }}
</script>